// 学生信息表数据模拟
require("../models/relation")
const mock = require("mockjs")
const {
    sequelize
} = require("../models/db")
const {
    StudentSInfon
} = require("../models/StudentSInfon")
const {
    StudentID
} = require("../models/StudentID")
const {
    Bednumber
} = require("../models/Bednumber")
const {
    RoomNo
} = require("../models/RoomNo")
const moment = require('moment');
const md5 = require("md5")
// 学号表数组
// let StuArr = []


async function init() {
    const classInfoId = await sequelize.query("select r.classInfoId,b.id from RoomNo r,Bednumber b where b.RoomNoId = r.id  and b.isPeople = 1")
    // console.log(classInfoId[0]);
    const statistics = classInfoId[0].reduce((a, b) => {
        a[b.classInfoId] = (a[b.classInfoId] || 0) + 1
        return a
    }, {})
    const studentList = [];
    for (const key in statistics) {
        const StudentIDItem = await StudentID.findAll({
            attributes: [ "studentId",['studentName', 'Nickname'], "classInfoId"],
            limit: statistics[key],
            where: {
                classInfoId: key
            }
        })
        studentList.push(...JSON.parse(JSON.stringify(StudentIDItem)))
    }

    let OrderNoArr = []
    for(let i = 0; i < studentList.length; i++){
        OrderNoArr.push(moment.utc() + "" + `${classInfoId[0][i].id}`.padStart(6,0))
    } 

    mock.Random.extend({
        phone: function () {
            let phonePrefixs = ['132', '135', '189', "151", "158"]
            return this.pick(phonePrefixs) + mock.mock(/\d{8}/)
        }
    })

    classInfoId[0].forEach((item, index) => {
       
        studentList[index].BednumberId = item.id
        studentList[index].phone = mock.mock("@phone")
        studentList[index].loginPwd =  md5("123456")
        studentList[index].avatar = "/upload/student.png"
        studentList[index].EndTime = +moment.utc().add(12,"M")
        studentList[index].checkInTime = +moment.utc()
        studentList[index].OrderNo = OrderNoArr[index] 
    })
    StudentSInfon.bulkCreate(studentList).then(data =>{
        console.log(data);
    }).catch(err => {
        console.log(err);
    })
}

init()